﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiFreeValue : ApiBase
    {
        public ApiFreeValue(Controller ctx) : base(ctx) { }


        //系统变量
        public void Do(string action)
        {
            switch (action)
            {
                case "get_website":
                    {
                        ResponseJson(new ObjectResult { data = FreeValue.Get("website") });
                        break;
                    }             
                case "delete":
                    {
                        string key = GetStringParam(new ReqStringParamOption { Name = "the_delete_email_id" });
                        Cd cd = new Cd();
                        cd.And("value ='{0}'",key);
                        cd.And("_key ='{0}'","email_id");
                        List<FreeValue> list = GetListResult<FreeValue>(cd.Render()).list;
                        int parent_id = list[0].parent_id;
                        Cd cd2 = new Cd();
                        cd2.And("parent_id ='{0}'",parent_id);
                        int row1=G.DB.DeleteByCondition<FreeValue>(cd2.Render());
                        Cd cd3 = new Cd();
                        cd3.And("id='{0}'",parent_id);
                        int row2=G.DB.DeleteByCondition<FreeValue>(cd3.Render());
                        if (row1 != 0 && row2 != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "删除成功" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "删除失败" });
                        }
                        break;
                    }
                case "get_list":
                    {
                        string key = GetStringParam(new ReqStringParamOption { Name = "key" });
                        var target = FreeValue.Get(key);
                        List<object> li1 = new List<object>();
                        List<Dictionary<string, string>> li2 = new List<Dictionary<string, string>>();
                        foreach (Object item in target)
                        {                       
                            li1.Add(item);                           
                        }
                        li1.RemoveRange(0, 3);
                        ResponseJson(new ListResult<object> { list = li1 });
                        //ResponseJson(new ObjectResult { data = FreeValue.Get(key) });
                        break;
                    }
                case "get":
                    {
                        string key = GetStringParam(new ReqStringParamOption { Name = "key" });
                        ResponseJson(new ObjectResult { data = FreeValue.Get(key) });
                        break;
                    }
                case "set":
                    {
                        string the_key = GetStringParam(new ReqStringParamOption { Name = "key", Level = REQPARAMLEVEL.FREE });
                        if (the_key != "") FreeValue.Set(the_key, req_value);
                        else
                        {
                            Dictionary<string, string> keyvalues = GetObjectParam<Dictionary<string, string>>(new ReqObjectParamOption<Dictionary<string, string>> { Name = "keyvalues" });
                            foreach (string key in keyvalues.Keys)
                            {
                                FreeValue.Set(key, keyvalues[key]);
                            }
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "get_check":
                    {
                        ResponseJson(new ObjectResult { data = FreeValue.Get("check") });
                        break;
                    }
                default: break;
            }
        }

    }
}